package com.neulogistics.contrib.usercenter.infrastructure.persistence.converter;

import cn.hutool.json.JSONUtil;
import com.neulogistics.contrib.usercenter.domain.model.aggregate.User;
import com.neulogistics.contrib.usercenter.domain.model.enumeration.UserRole;
import com.neulogistics.contrib.usercenter.domain.model.enumeration.UserStatus;
import com.neulogistics.contrib.usercenter.infrastructure.persistence.orm.po.UserPO;
import org.springframework.beans.BeanUtils;

public class UserEntityPOConverter {
    public static UserPO entityToPO(User user){
        if(user == null)
            return null;

        UserPO userPO = new UserPO();
        // 复制普通属性
        BeanUtils.copyProperties(user, userPO);

        // 枚举值转化
        if(user.getUserRole() != null){
            userPO.setUserRole(user.getUserRole().id);
        }
        if(user.getUserStatus() != null){
            userPO.setUserStatus(user.getUserStatus().id);
        }

        return userPO;
    }

    public static User POToEntity(UserPO userPO){
        if(userPO == null)
            return null;

        User user = new User();
        // 复制普通属性
        BeanUtils.copyProperties(userPO, user);

        // 枚举值转化
        user.setUserRole(UserRole.fromValue(
                userPO.getUserRole(), UserRole.class));
        user.setUserStatus(UserStatus.fromValue(
                userPO.getUserStatus(), UserStatus.class));

        return user;
    }
}
